﻿using System;
using System.Net;
using System.Text;
using System.Linq;
using System.Collections.Generic;

namespace TagSearch.Plugins
{
    public class Scheme : IClass
    {
        #region JsonParseData
        #endregion
        protected Dictionary<String, int> _TempTotal;
        protected Dictionary<String, List<ITag>> _TempTag;
        private object _Lock = new object();
        public Scheme()
        {
            JParse = new System.Text.Json.JsonParser();
            _TempTotal = new Dictionary<string, int>();
            _TempTag = new Dictionary<String, List<ITag>>();
        }
        public override void Search(string Name)
        {
            GetInfo(Name);
        }
        protected override void GetInfo(string Name)
        {
            lock (_Lock)
            {
                _TempTotal.Add(Name, -1);
                _TempTag.Add(Name, new List<ITag>());
            }
            var web = new IWebClient();
            web.DownloadDataCompleted += DownloadDataCompleted;
            web.DownloadDataAsync(new Uri(""), new IWebClient.WebClientState(Name, 1, null));

            while (_TempTotal[Name] != _TempTag[Name].Count) { System.Threading.Thread.Sleep(1000); }

            OnEvent(Name,_TempTag[Name]);

            _TempTotal.Remove(Name);
            _TempTag.Remove(Name);
            base.GetInfo(Name);
        }
        protected void DownloadDataCompleted(dynamic sender, dynamic e)
        {
            if (e.Result != null)
            {
                string Name = e.UserState.Name;
                switch ((int)e.UserState.State)
                {
                    case 1: break;
                }
            }
        }
    }
}
